/**
 * @file
 * @auth jinguangguo
 * @date 2016/10/19
 */


import cookie from './cookie'
import '../common/codeMap';
import {LANG, LOCAL} from './lang';

const DEFAULT_BROKER_ID = 10003;

const DEFAULT_TYPE = 'POST';
const DEFAULT_TIMEOUT = 30000;

let TIP_EXCEPTION = 'Network exception, please try again later.';
if (LANG === LOCAL.cn) {
    TIP_EXCEPTION = '网络异常，请稍候重试';
}else if(LANG === LOCAL.es){
    TIP_EXCEPTION = 'Excepción de red, por favor inténtelo de nuevo más tarde';
}else if(LANG === LOCAL.hk){
    TIP_EXCEPTION = '網絡異常，請稍後重試';
}

// const UN_LOGIN_LIST = [
//     'index.html',
//     'm_register.html',
//     'm_reset.html',
// ];

const LOGIN_LIST = [
    'assets',
    'deposit',
    'withdraw',
    'account',
    'new-address'
];

function doLogin() {

    // 清理cookiei
    cookie.remove('LOGIN_TOKEN',{path:'/'});
    window.localStorage.removeItem('account-no');
    window.localStorage.removeItem('risk-tip');

    if (window.app && window.app.DEBUG === true) {
        console.warn('No Login');
    } else {
        // 首页
        // if (window.location.pathname === '/') {
        //     return;
        // }
        // let item;
        // for (let i = 0, len = UN_LOGIN_LIST.length; i < len; i++) {
        //     item = UN_LOGIN_LIST[i];
        //     if (window.location.href.indexOf(item) >= 0) {
        //         return;
        //     }
        // }

        // setTimeout(() => {
        // window.originAlert('未登录');
        // window.location.href = './login.html?from=' + from;
        // }, 100);

        let item;
        let from = window.encodeURIComponent(window.location.href);
        for (let i = 0, len = LOGIN_LIST.length; i < len; i++) {
            item = LOGIN_LIST[i];
            if (window.location.hash.indexOf(item) >= 0) {
                window.location.href = '/f/m_login.html?from=' + from;
                break;
            }
        }
    }
}

function doException(xhr, showError) {
    console.log('【' + TIP_EXCEPTION + '】*************** ', JSON.stringify(xhr), ' ***************');
    if (showError ===  true) {
        window.error(TIP_EXCEPTION);
    }
}

function doResult(rep, deferred) {
    if (rep.code === window.CODE.SUCCESS) {
        deferred.resolveWith(this, [rep]);
        return;
    } else if (rep.code === window.CODE.NO_LOGIN.CODE) {
        doLogin();
        deferred.resolveWith(this, [rep]);
        return;
    } else if (rep.code === window.CODE.REQUEST_ERROR.CODE) {
        if (LANG === LOCAL.cn) {
            window.error(window.CODE.REQUEST_ERROR.cn);
            deferred.rejectWith(this, [window.CODE.REQUEST_ERROR.cn]);
        }else if (LANG === LOCAL.us){
            window.error(window.CODE.REQUEST_ERROR.us);
            deferred.rejectWith(this, [window.CODE.REQUEST_ERROR.us]);
        } else if (LANG === LOCAL.es) {
            window.error(window.CODE.REQUEST_ERROR.es);
            deferred.rejectWith(this, [window.CODE.REQUEST_ERROR.es]);
        }else if (LANG === LOCAL.hk) {
            window.error(window.CODE.REQUEST_ERROR.hk);
            deferred.rejectWith(this, [window.CODE.REQUEST_ERROR.hk]);
        }
        return;
    } else if (rep.code === window.CODE.SERVER_ERROR.CODE) {
        if (LANG === LOCAL.cn) {
            window.error(window.CODE.SERVER_ERROR.cn);
            deferred.rejectWith(this, [window.CODE.SERVER_ERROR.cn]);
        }else if (LANG === LOCAL.us){
            window.error(window.CODE.SERVER_ERROR.us);
            deferred.rejectWith(this, [window.CODE.SERVER_ERROR.us]);
        } else if (LANG === LOCAL.es) {
            window.error(window.CODE.SERVER_ERROR.es);
            deferred.rejectWith(this, [window.CODE.SERVER_ERROR.es]);
        }else if (LANG === LOCAL.hk) {
            window.error(window.CODE.SERVER_ERROR.hk);
            deferred.rejectWith(this, [window.CODE.SERVER_ERROR.hk]);
        }
        return;
    } else if (rep.code === window.CODE.NO_PERMISSION.CODE || rep.code === window.CODE.TOKEN_INVALID.CODE) {
        doLogin();
        return;
        // if (LANG === LOCAL.cn) {
        //     window.error(window.CODE.NO_PERMISSION.cn);
        //     deferred.rejectWith(this, [window.CODE.SERVER_ERROR.cn]);
        // }else if (LANG === LOCAL.us){
        //     window.error(window.CODE.NO_PERMISSION.us);
        //     deferred.rejectWith(this, [window.CODE.SERVER_ERROR.us]);
        // } else if (LANG === LOCAL.es) {
        //     window.error(window.CODE.NO_PERMISSION.es);
        //     deferred.rejectWith(this, [window.CODE.SERVER_ERROR.es]);
        // }else if (LANG === LOCAL.hk) {
        //     window.error(window.CODE.NO_PERMISSION.hk);
        //     deferred.rejectWith(this, [window.CODE.SERVER_ERROR.hk]);
        // }
        // return;
    }
    // 非常规接口会走这儿
    deferred.resolveWith(this, [rep]);
}

function stringifyHeader(param) {
    let results = [];
    if ($.isEmptyObject(param) === true) {
        return '';
    }
    $.map(param, (value, key) => {
        let valueTemp = window.encodeURIComponent(value);
        results.push(`${key}=${valueTemp}`);
    });
    return results.join(',');
}

function getAuthHeader(param) {
    let headers = {
        'accept-language': LANG
    };
    // let accountNo = window.localStorage.getItem('account-no');
    // let auth;
    // if (accountNo) {
    //     auth = {
    //         'account-no': accountNo
    //     };
    // }

    let token = cookie.get('LOGIN_TOKEN');
    // if (!token) {
    //     window.location.href = './login.html';
    //     return;
    // }
    let auth = {};
    if (token) {
        auth.token = token;
    }

    if (param.header && $.isEmptyObject(param.header) === false) {
        auth = $.extend(auth , param.header);
    }
    if ($.isEmptyObject(auth) === false) {
        headers.authorization = stringifyHeader(auth);
    }
    return headers;
}

export default {

    /**
     * 发送请求
     * @param path
     * @param param
     * @param showError
     * @returns {*}
     */
    send(path, param = {}, showError = true) {
        let deferred = $.Deferred();

        param.data = $.extend(param.data, {
            brokerId: DEFAULT_BROKER_ID
        });

        $.ajax({
            url: (param.baseUri || '') + path,
            data: JSON.stringify(param.data),
            type: param.type || DEFAULT_TYPE,
            dataType: 'JSON',
            headers: {
                'accept-language': LANG
            },
            contentType: 'application/json; charset=UTF-8',
            async: param.async!==false,
            timeout: param.timeout || DEFAULT_TIMEOUT,
            beforeSend() {
                window.loading();
            },
            success(rep) {
                doResult(rep, deferred);
            },
            error(xhr, type) {
                deferred.rejectWith(this, [TIP_EXCEPTION]);
                doException(xhr, showError);
            },
            complete() {
                window.unloading();
            }
        });

        return deferred.promise();
    },

    /**
     * 站外请求
     * @param path
     * @param param
     */
    sendOutside(path, param = {}) {
        let deferred = $.Deferred();

        param.data = $.extend(param.data, {
            brokerId: DEFAULT_BROKER_ID
        });

        $.ajax({
            url: (param.baseUri || '') + path,
            data: $.param(param.data),
            type: param.type || DEFAULT_TYPE,
            dataType: 'JSON',
            headers: {
                'accept-language': LANG
            },
            contentType: 'application/json; charset=UTF-8',
            async: param.async!==false,
            timeout: param.timeout || DEFAULT_TIMEOUT,
            beforeSend() {
                window.loading();
            },
            success(rep) {
                deferred.resolveWith(this, [rep]);
            },
            error(xhr, type) {
                deferred.rejectWith(this, [TIP_EXCEPTION]);
                doException(xhr);
            },
            complete() {
                window.unloading();
            }
        });

        return deferred.promise();
    },

    /**
     * quote http接口
     * @param path
     * @param param
     */
    getHttpQuote(path, param = {}) {
        let deferred = $.Deferred();

        // let baseUri = '//quote.ourdax.com';
        let baseUri = '';
        if(window.location.href.indexOf('localhost') >= 0 || window.location.href.indexOf('gdae2.dev.gpdax.com') >= 0){
            baseUri = '';
        }

        param.data = $.extend(param.data, {
            brokerId: DEFAULT_BROKER_ID
        });

        $.ajax({
            url: baseUri + path,
            data: $.param(param.data),
            type: 'GET',
            dataType: 'JSON',
            headers: {
                'accept-language': LANG
            },
            contentType: 'application/json; charset=UTF-8',
            async: param.async!==false,
            timeout: param.timeout || DEFAULT_TIMEOUT,
            success(rep) {
                deferred.resolveWith(this, [rep]);
            },
            error(xhr, type) {
                deferred.rejectWith(this, [TIP_EXCEPTION]);
                doException(xhr);
            }
        });

        return deferred.promise();
    },


    /**
     * smcf1 接口
     * @param path
     * @param param
     */
    getSMCF1History(path, param = {}) {
        let deferred = $.Deferred();

        let baseUri = 'http://35.201.202.42';

        param.data = $.extend(param.data, {
            brokerId: DEFAULT_BROKER_ID
        });

        $.ajax({
            url: baseUri + path,
            data: $.param(param.data),
            type: 'GET',
            dataType: 'JSON',
            headers: {
                'accept-language': LANG
            },
            contentType: 'application/json; charset=UTF-8',
            async: param.async!==false,
            timeout: param.timeout || DEFAULT_TIMEOUT,
            success(rep) {
                deferred.resolveWith(this, [rep]);
            },
            error(xhr, type) {
                deferred.rejectWith(this, [TIP_EXCEPTION]);
                doException(xhr);
            }
        });

        return deferred.promise();
    },

    /**
     * 发送请求get
     * @param path
     * @param param
     * @param showError
     */
    get(path, param={}, showError = true) {
        param.data = $.extend(param.data, {
            brokerId: DEFAULT_BROKER_ID
        });

        let deferred = $.Deferred();

        let headers = getAuthHeader(param);

        let a = $.ajax({
            url: (param.baseUri || '') + path,
            data: $.param(param.data),
            type: 'GET',
            headers: headers,
            contentType: 'application/json; charset=UTF-8',
            async: param.async!==false,
            dataType: 'JSON',
            timeout: param.timeout || DEFAULT_TIMEOUT,
            beforeSend() {
                window.loading();
            },
            success(rep) {
                doResult(rep, deferred);
                // console.table(a.responseText)
            },
            error(xhr, type) {
                deferred.rejectWith(this, [TIP_EXCEPTION]);
                doException(xhr, showError);
            },
            complete() {
                window.unloading();
            }
        });

        return deferred.promise();
    },

    /**
     * 发送请求post
     * @param path
     * @param param
     * @param showError
     * @returns {*}
     */
    post(path, param, showError = true) {

        param.data = $.extend(param.data, {
            brokerId: DEFAULT_BROKER_ID
        });

        let deferred = $.Deferred();
        
        let headers = getAuthHeader(param);

        $.ajax({
            url: (param.baseUri || '') + path,
            data: JSON.stringify(param.data),
            type: 'POST',
            dataType: 'JSON',
            headers: headers,
            contentType: 'application/json; charset=UTF-8',
            async: param.async!==false ,
            timeout: param.timeout || DEFAULT_TIMEOUT,
            beforeSend() {
                window.loading();
            },
            success(rep) {
                doResult(rep, deferred);
            },
            error(xhr, type) {
                deferred.rejectWith(this, [TIP_EXCEPTION]);
                doException(xhr, showError);
            },
            complete() {
                window.unloading();
            }
        });

        return deferred.promise();
    },

    /**
     * 删除方法走这里
     * @param path
     * @param param
     * @param showError
     * @returns {*}
     */
    delete(path, param, showError = true) {

        param.data = $.extend(param.data, {
            brokerId: DEFAULT_BROKER_ID
        });

        let deferred = $.Deferred();

        let headers = getAuthHeader(param);

        $.ajax({
            url: (param.baseUri || '') + path,
            data: $.param(param.data),
            type: 'GET',
            dataType: 'JSON',
            headers: headers,
            contentType: 'application/json; charset=UTF-8',
            async: param.async!==false,
            timeout: param.timeout || DEFAULT_TIMEOUT,
            beforeSend() {
                window.loading();
            },
            success(rep) {
                doResult(rep, deferred);
            },
            error(xhr, type) {
                deferred.rejectWith(this, [TIP_EXCEPTION]);
                doException(xhr, showError);
            },
            complete() {
                window.unloading();
            }
        });

        return deferred.promise();
    }

};